import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;

import java.util.Arrays;
import java.util.Properties;

public class KafkaRate {

    public static void main(String[] args) {
        if(args.length==0){
            System.out.println("请输入参数:[topic] [partition] [fromOffset] [messages] [server] [maxRecords]");
        }
        String topic = args[0];
        int partition = Integer.parseInt(args[1]);
        long fromOffset = Long.parseLong(args[2]);
        int messages = Integer.parseInt(args[3]);
        String maxRecords = args[5];
        String server = args[4];
        Properties prop = new Properties();
        prop.put("bootstrap.servers",server);
        prop.put("group.id","test8");
        prop.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        prop.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        prop.put("max.poll.records",maxRecords);
        Consumer consumer = new KafkaConsumer(prop);
        TopicPartition p = new TopicPartition(topic,partition);
        consumer.assign(Arrays.asList(p));
        consumer.seek(p,fromOffset);
        while(true) {
            long start = System.currentTimeMillis();
            int i = 0;
            while (i < messages) {
                ConsumerRecords<String, String> c = consumer.poll(100);
                for (ConsumerRecord<String, String> c1 : c) {
                    if (i < messages) {
//                    System.out.println("Key: " + c1.key() + " Value: " + c1.value() + " Offset: " + c1.offset() + " Partitions: " + c1.partition());
                        i++;
                    }
                }
            }
            System.out.println(System.currentTimeMillis() - start);
        }

    }
}
